package com.zoer.hibdao;

// default package

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.zoer.domain.Node;

/**
 * A data access object (DAO) providing persistence and search support for Node
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see .Node
 * @author MyEclipse Persistence Tools
 */

public class NodeDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(NodeDAO.class);
	// property constants
	public static final String NODE_ID = "nodeId";
	public static final String PARENT_ID = "parentId";
	public static final String HREF_ADDRESS = "hrefAddress";
	public static final String NODE_NAME = "nodeName";
	public static final String SYS = "sys";

	public void save(Node transientInstance) {
		log.debug("saving Node instance");
		try {
			Session s = getSession();
			s.beginTransaction();
			s.save(transientInstance);
			s.getTransaction().commit();
			s.close();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Node persistentInstance) {
		log.debug("deleting Node instance");
		try {
			Session s = getSession();
			s.beginTransaction();
			s.delete(persistentInstance);
			s.getTransaction().commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Node findById(java.lang.Integer id) {
		log.debug("getting Node instance with id: " + id);
		try {
			Node instance = (Node) getSession().get("com.zoer.domain.Node", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Node instance) {
		log.debug("finding Node instance by example");
		try {
			List results = getSession().createCriteria("com.zoer.domain.Node")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findBySys(Object sys) {
		return findByProperty(SYS, sys);
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Node instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from com.zoer.domain.Node as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByNodeId(Object nodeId) {
		return findByProperty(NODE_ID, nodeId);
	}

	public List findByParentId(Object parentId) {
		return findByProperty(PARENT_ID, parentId);
	}

	public List findByHrefAddress(Object hrefAddress) {
		return findByProperty(HREF_ADDRESS, hrefAddress);
	}

	public List findByNodeName(Object nodeName) {
		return findByProperty(NODE_NAME, nodeName);
	}

	public List findAll() {
		log.debug("finding all Node instances");
		try {
			String queryString = "from com.zoer.domain.Node";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Node merge(Node detachedInstance) {
		log.debug("merging Node instance");
		try {
			Node result = (Node) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Node instance) {
		log.debug("attaching dirty Node instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Node instance) {
		log.debug("attaching clean Node instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}